package org.yoki.edu.common.utils.random;

import java.util.Random;

/**
 * @author Sky$
 * @Description: TODO
 * @date 2017/10/30$ 10:10$
 */
public class PhoneCodeUtils {

    public static final int DEFAULT_LENGTH = 4;

    public static String getCode() {
        return getCode(DEFAULT_LENGTH);
    }

    public static String getCode(Integer length) {
        if (null == length) {
            length = DEFAULT_LENGTH;
        }
        Random random = new Random();
        Integer num = new Integer(random.nextInt(((Double) Math.pow(10, length)).intValue()));
        StringBuffer buf = new StringBuffer(num.toString()).reverse() ;
        int subLength = (length - num.toString().length()) ;
        for(int i = 0 ; i <subLength ; i ++ ){
            buf.append("0") ;
        }
        return buf.reverse().toString();
    }

    public static void main(String[] args) {
        for(int i = 0 ; i < 1000 ; i++){
            String code = getCode(6) ;
//            if(code.length()  6) {
                System.out.println(code);
//            }
        }
    }

}
